package presentation;

import java.awt.Color;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.dnd.DnDConstants;
import java.awt.dnd.DropTargetDragEvent;
import java.awt.dnd.DropTargetDropEvent;
import java.awt.dnd.DropTargetEvent;
import java.awt.dnd.DropTargetListener;
import java.awt.dnd.InvalidDnDOperationException;
import java.io.IOException;

import solitaire.application.Tas;

/**
 * DropTargetListener pour PTasDeCartesColorees
 * @author Guillaume Benitez, Kevin Lemaux
 *
 */
public class ColoreesDropTargetListener implements DropTargetListener {
	
	private PTasDeCartesColorees coloree;

	public ColoreesDropTargetListener(PTasDeCartesColorees coloree) {
		this.coloree=coloree;
	}

	@Override
	public void dragEnter(DropTargetDragEvent event) {
		Transferable transferable=event.getTransferable();
		try {
			PTasDeCartesTransferable tas=(PTasDeCartesTransferable) transferable.getTransferData(new DataFlavor(DataFlavor.javaJVMLocalObjectMimeType));
			if(coloree.controle.isEmpilable((Tas)tas.getControle())){
				coloree.setBackground(Color.green);
			}else{
				coloree.setBackground(Color.red);
			}
		} catch (UnsupportedFlavorException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Override
	public void dragExit(DropTargetEvent arg0) {
		coloree.setBackground(new Color(46, 139, 87));
	}

	@Override
	public void dragOver(DropTargetDragEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void drop(DropTargetDropEvent event) {
		try {
			Transferable transferable=event.getTransferable();
			if(event.isLocalTransfer()){
				if(transferable.isDataFlavorSupported(new DataFlavor(DataFlavor.javaJVMLocalObjectMimeType))){
					PTasDeCartesTransferable tas=(PTasDeCartesTransferable) transferable.getTransferData(new DataFlavor(DataFlavor.javaJVMLocalObjectMimeType));
					if(tas.getControle().getNombre()>1){
						event.rejectDrop();
					}else if(coloree.controle.isEmpilable(tas.getControle().getSommet())){
						event.acceptDrop(DnDConstants.ACTION_MOVE);
						coloree.controle.empiler((Tas)tas.getControle());
						event.getDropTargetContext().dropComplete(true);
					}else{
						event.rejectDrop();
					}
				}else{
					event.rejectDrop();
				}

			}else{
				event.rejectDrop();
			}
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			event.rejectDrop();
		} catch (UnsupportedFlavorException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			event.rejectDrop();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			event.rejectDrop();
		} catch (InvalidDnDOperationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		coloree.setBackground(new Color(46, 139, 87));
	}

	@Override
	public void dropActionChanged(DropTargetDragEvent arg0) {
		// TODO Auto-generated method stub

	}

}
